#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#include <syslog.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <time.h>
#include "TCPClient.h"
#include <Log.h>

#include "StorageProtocol.h"

#define SERVER_PORT "5000"
#define SERVER_IP   "127.0.0.1"

#include <Configuration.h>
static Configuration *cfg = NULL;

void RunThread()
{
    try{
        while(!boost::this_thread::interruption_requested())
        {
            INFO("Connecting to server - %s:%s", cfg->get<std::string>("config.server.ip", SERVER_IP).c_str(), cfg->get<std::string>("config.server.cport", SERVER_PORT).c_str());
            try{
                boost::shared_ptr< CSNetworkLib::TCPClient<StorageProtocol>  > client(new CSNetworkLib::TCPClient<StorageProtocol>(cfg->get<std::string>("config.server.ip", SERVER_IP).c_str(), cfg->get<std::string>("config.server.cport", SERVER_PORT).c_str()));
                INFO("..Connected, Authorization started...");
                if (!client->GetProtocol().Auth(cfg->get<std::string>("config.storage.name", ""), cfg->get<std::string>("config.storage.key", "")))
                {
                    ERR("Auth failed!");
                }
                else
                {
                    while(client->IsActive() && !boost::this_thread::interruption_requested())
                    {
                        boost::this_thread::sleep(boost::posix_time::milliseconds(100));
                    }
                    ALERT("Connection lose");
                }
            }catch(...)
            {
                ERR("Connection failed");
            }
            boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
        }
	}catch(const std::exception &e)
	{
		ERR("THREAD EXCEPTION: %s", e.what());
	}
}

int32_t main(int32_t argc, char *argv[])
{
    Configuration gcfg("", (((argc>1) && (argv[1][0]!=0))?argv[1]:"config"));
    if (!gcfg.isLoaded())
    {
        printf("ERROR: Config file: '%s' [.xml] not found",(((argc>1) && (argv[1][0]!=0))?argv[1]:"config"));
        return 1;
    }
    cfg = &gcfg;
    std::string tmp = cfg->get<std::string>("config.log.file", "");
    if (tmp.empty())
    {
        initLog();
    }
    else
    {
        initLog(tmp.c_str(), false);
    }
    uint32_t ret = 0;
    do
	{
        FUNCTION_TRACK
        try{
            boost::thread workerThread(RunThread);
            printf("Press ENTER key to break...\n");
            do
            {
                int c = getchar();
                if (13 == c || 10 == c) break;
                boost::this_thread::sleep(boost::posix_time::milliseconds(500));
            } while(true);
            workerThread.interrupt();
            boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
        }catch(const std::exception &e)
        {
            ERR("MAIN EXCEPTION: %s", e.what());
        }
	} while(false);
	INFO("Application shutdown");
	shutdownLog();
	return 0;
}
